<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<button> 存储 cookie </button>
	<br>
	<button> 取对象 </button>
	<button> 取值 </button>
	<script>
		function getCookie( key ) {

			// 返回值要么是一个字符串, 要么是一个对象

			// cookie 取值形如 'k=v; k=v; k=v'
			var strCookie = document.cookie;
			if ( !strCookie ) return null;
			var arrCookies = strCookie.split( '; ' );
			// 使用正则处理形如 'k=v' 的字符串
			var r = /(.+)=(.+)/;
			var tmp = {};
			for ( var i = 0; i < arrCookies.length; i++ ) {
				var cookie = arrCookies[ i ];
				var m = r.exec( cookie );
				// tmp[ m[ 1 ] ] = m[ 2 ]; // 需要考虑 decodeURI() 函数处理一下
				tmp[ m[ 1 ] ] = decodeURI( m[ 2 ] );
			}
			return key ? tmp[ key ] : tmp;
		}


		// 
		var btn = document.getElementsByTagName( 'button' )[ 0 ];
		btn.onclick = function () {

			document.cookie = 'itcast=' + encodeURI( '传智播客' );
			document.cookie = 'itheima=' + encodeURI( '黑马程序员' ); 
		};

		var get1 = document.getElementsByTagName( 'button' )[ 1 ];


		var get2 = document.getElementsByTagName( 'button' )[ 2 ];


		get1.onclick = function () {
			// 取对象
			console.log( getCookie() );
		};


		get2.onclick = function () {
			// 取值
			console.log( getCookie( 'itheima' ) );


			console.log( getCookie( 'itcast' ) );
		};


	</script>
</body>
</html>